package com.definesys.smecrailtransit.module.Navigation;

import android.content.Context;

import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.BaseSubscriber;
import com.definesys.smecrailtransit.SmecManager.SmecRetrofit.HttpResult;
import com.definesys.smecrailtransit.api.SmecRailConstant;
import com.definesys.smecrailtransit.base.SmecBasePresenter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleAttentionBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleLineBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleStation;
import com.definesys.smecrailtransit.utils.CommonUtils;

import java.util.ArrayList;
import java.util.HashMap;

import io.realm.RealmResults;
import rx.Observable;

/**
 * Created by cjy on 2017/8/13.
 */

public class NavigationPresenter extends SmecBasePresenter {

    public NavigationPresenter(Context mContext) {
        super(mContext);
    }

    public void getAttention(HashMap<String, Object> map) {
        Observable<HttpResult<EleAttentionBean>> observable=smecRailService.getAttention(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<EleAttentionBean>>() {
            @Override
            public void onNext(HttpResult<EleAttentionBean> eleAttentionBeanHttpResult) {
                if(eleAttentionBeanHttpResult.getData()!=null){
                    realmManager.deleteAll(EleAttentionBean.class);
                    realmManager.insertOrUpdate(eleAttentionBeanHttpResult.getData());
                }
            }
        });
    }

    public void getAllStations(HashMap<String, Object> map){

//        RealmResults<EleLineBean> list=(RealmResults<EleLineBean>)realmManager.findAll(EleLineBean.class);
//        RealmResults<EleStation> realmResults=(RealmResults<EleStation>)realmManager.findAll(EleStation.class);

//        if(CommonUtils.notEmpty(list) && CommonUtils.notEmpty(realmResults)){
//            ArrayList<EleLineBean> eleLineBeanArrayList=(ArrayList<EleLineBean>)realmManager.getRealm().copyFromRealm(list);
//            ArrayList<EleStation> eleStationArrayList=(ArrayList<EleStation>)realmManager.getRealm().copyFromRealm(realmResults);
//            for(EleLineBean eleLineBean : eleLineBeanArrayList){
//                ArrayList<EleStation> lineStations=new ArrayList<>();
//                for(EleStation eleStations :eleStationArrayList){
//                    if(eleLineBean.getLineId() == eleStations.getLineId()){
//                        lineStations.add(eleStations);
//                    }
//                }
//                eleLineBean.setLineStations(lineStations);
//            }
//            SmecRailConstant.initConstant().setEleLineBeanArrayList(eleLineBeanArrayList);
//        }else {
//
//
//
//        }

        Observable<HttpResult<ArrayList<EleLineBean>>> observable=smecRailService.getAllStations(map);
        schedulerThread(observable).subscribe(new BaseSubscriber<HttpResult<ArrayList<EleLineBean>>>() {
            @Override
            public void onNext(HttpResult<ArrayList<EleLineBean>> eleLineBeanHttpResult) {
                ArrayList<EleLineBean> arrayList=eleLineBeanHttpResult.getData();
                realmManager.deleteAll(EleLineBean.class);
                realmManager.deleteAll(EleStation.class);
                realmManager.insertOrUpdateBatch(arrayList);
                if(CommonUtils.notEmpty(arrayList)){
                    for(int i =0;i<arrayList.size();i++){
                        realmManager.insertOrUpdateBatch(arrayList.get(i).getLineStations());
                    }
                }
                SmecRailConstant.initConstant().setEleLineBeanArrayList(arrayList);
            }
        });

    }
}
